<?php

require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['season']) && isset($_POST['gamenumber']) && isset($_POST['playerID'])) {

  include_once './db_bb_connect.php';
	
	$season = $_POST['season'];
	$gamenumber = $_POST['gamenumber'];
	$playerID = $_POST['playerID'];

	$sql = "SELECT * 
	FROM stats 
	WHERE season = '$season' 
	AND gamenumber = '$gamenumber'
	AND playerID = '$playerID'";

	$result = $db->query($sql);

	$row = $result->fetch_assoc();

	  $DRB = $row['DRB']; 
  	$ORB = $row['ORB']; 
  	$TOV = $row['TOV']; 
  	$ST = $row['ST']; 
    $AST = $row['AST']; 
    $BS = $row['BS']; 
  	$FTscored = $row['FTscored']; 
  	$FTtotal = $row['FTtotal']; 
  	$FGscored = $row['FGscored']; 
  	$FGtotal = $row['FGtotal'];
  	$PF = $row['PF'];
  	$DNP = $row['DNP'];
  	$FGoutsidescored = $row['FG_outside_scored']; 
  	$FGoutsidetotal = $row['FG_outside_total'];
  	$THPscored = $row['THPscored']; 
  	$THPtotal = $row['THPtotal'];
    $S5 = $row['S5'];

    $minutesplayed = floor((int)$row['MP'] / 60);
    $seconds = (int)$row['MP'] % 60;

    if ( $seconds >= 0 && $seconds < 10) {
      $secondsplayed = "0$seconds";
    } else {
      $secondsplayed = $seconds;
    }

    $MP = "$minutesplayed:$secondsplayed"; 

  $sql2 = "SELECT firstname, lastname 
  FROM users 
  WHERE user_key = '$playerID'";

  $result2 = $db->query($sql2);
  $row2 = $result2->fetch_assoc();
  $firstname = $row2['firstname']; 
  $lastname = $row2['lastname'];

  $sql3 = "SELECT opponent, location 
  FROM games 
  WHERE season = '$season'
  AND gamenumber = '$gamenumber'";

  $result3 = $db->query($sql3);
  $row3 = $result3->fetch_assoc();
  $opponent = $row3['opponent']; 
  $location = $row3['location'];


}

?>

<div class="container-fluid">
<div class="row">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-6 col-lg-6">

<center><h4>Edit stats of <?= $firstname; ?> <?= $lastname; ?> <?= $season; ?> game <?= $gamenumber; ?> - <?= $opponent; ?> <?= $location; ?></h4> </center>
<br>

<form>
 <div class="form-group">

 	<input type="hidden" name="season" value="<?= $season; ?>"/>

  <input type="hidden" name="playerID" value="<?= $playerID; ?>"/>
  
  <input type="hidden" name="gamenumber" value="<?= $gamenumber; ?>"/>


	<div class="input-group">
        <input class="form-control" id="ex1" type="number" placeholder="DRB" name="DRB" value="<?= $DRB; ?>">
       <span class="input-group-addon">DRB</span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex2" type="number" placeholder="ORB" name="ORB" value="<?= $ORB; ?>">
        <span class="input-group-addon">ORB</span>
      </div>
      <br/>
     <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="TOV" name="TOV" value="<?= $TOV; ?>">
        <span class="input-group-addon">TO</span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="ST" name="ST" value="<?= $ST; ?>">
        <span class="input-group-addon">ST</span>
      </div>
      <br/>
<?php

if ($season >= '2018/2019') {
  
?>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="AST" name="AST" value="<?= $AST; ?>">
        <span class="input-group-addon">AST</span>
      </div>
      <br/>

      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="BS" name="BS" value="<?= $BS; ?>">
        <span class="input-group-addon">BS</span>
      </div>
      <br/>




<?php } ?>




<?php

if ($season >= '2017/2018') {
    $spantextscored = "FGI scored";
    $placeholderscored = "FG Inside scored";
    $spantexttotal = "FGI total";
    $placeholdertotal = "FG Inside total";
  } else {
    $spantextscored = "FG scored";
    $placeholderscored = "Field Goals scored";
    $spantexttotal = "FG total";
    $placeholdertotal = "Field Goals total";
  }
?>     
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="<?= $placeholderscored; ?>" name="FGscored" value="<?= $FGscored; ?>">
        <span class="input-group-addon"><?= $spantextscored; ?>
          
        </span>
      </div>
      <br/>
      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="<?= $placeholdertotal; ?>" name="FGtotal" value="<?= $FGtotal; ?>">
        <span class="input-group-addon"><?= $spantexttotal; ?>
        </span>
      </div>
      <br/>
<?php

if ($season >= '2017/2018') {
  
?>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Outside scored" name="FGoutsidescored" value="<?= $FGoutsidescored; ?>">
        <span class="input-group-addon">FGO scored</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FG Outside total" name="FGoutsidetotal" value="<?= $FGoutsidetotal; ?>">
        <span class="input-group-addon">FGO total</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="3p scored" name="THPscored" value="<?= $THPscored; ?>">
        <span class="input-group-addon">3p scored</span>
</div>
<br/>

<div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="3p total" name="THPtotal" value="<?= $THPtotal; ?>">
        <span class="input-group-addon">3p total</span>
</div>
<br/>

      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="FT scored" name="FTscored" value="<?= $FTscored; ?>">
        <span class="input-group-addon">FT scored</span>
      </div>
      <br/>
      <div class="input-group">
       <input class="form-control" id="ex3" type="number" placeholder="FT total" name="FTtotal" value="<?= $FTtotal; ?>">
         <span class="input-group-addon">FT total</span>
      </div>
      <br/>

<?php
}

?>

      <div class="input-group">
        <input class="form-control" id="ex3" type="number" placeholder="PF" name="PF" value="<?= $PF; ?>">
        <span class="input-group-addon">PF</span>
              </div>
      <br/>

<?php

if ($season >= '2018/2019') {

?>

<div class="input-group">
        <input class="form-control" id="ex3" type="text" placeholder="Minutes Played 00:00" name="MP" value="<?= $MP; ?>">
        <span class="input-group-addon">MP</span>
</div>
<br/>


<div class="form-group">
  <label for="ex3">Starting 5:</label>
        <select class="form-control" name="S5">
          <option value="<?= $S5; ?>" selected="selected"><?= $S5; ?> was selected</option>
          <option value="yes">yes</option>
          <option value="no">no</option>
        </select>
</div>
<br/>

<?php
}

?>


      <div class="form-group">
        <label for="ex3">DNP</label>
        <select name="DNP" class="form-control">
          <option value="<?= $DNP; ?>" selected="selected"><?= $DNP; ?></option>
		  <option value="no">no</option>
          <option value="yes">DNP</option>
          <option value="coach"> DNP coach</option>
          <option value="injury">DNP injury</option>
        </select>
      </div>

      <button class="btn btn-default editplayerstatssubmitbutton" type="submit">Update Entry</button>

</div>

</form>

</div>

<div class="col-md-3 col-lg-3"></div>
</div>
</div>

<script type="text/javascript">

$(".editplayerstatssubmitbutton").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
      type: "POST",
        url: "./includes/handlers/editplayerstats_handler.php",
        data: postData,
        success: function(data) {  
          $("#entirestatspage").html(data);
          
        },
        error: function (xhr, status, error) {
          $("#entirestatspage").html(xhr.responseText);
        }
      }); 
    e.preventDefault();
});

</script>